package com.nkbh.sevenday.mykotlin.activity

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.nkbh.sevenday.mykotlin.R
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btnLogin -> login()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        btnLogin.setOnClickListener(this)
        btnRegister.setOnClickListener {
            startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
        }

    }


    fun login(){

        val prefs: SharedPreferences by lazy { this.getSharedPreferences("USER", Context.MODE_PRIVATE) }
        val username = prefs.getString("username", "")
        val key = prefs.getString("key", "")
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(key)){
            Toast.makeText(this@LoginActivity,"请先注册",Toast.LENGTH_SHORT).show()
            return
        }
        if (etUsername.text.toString().trim().equals(username) && etPwd.text.toString().trim().equals(key)){
            startActivity(Intent(this@LoginActivity, MainActivity::class.java))
            finish()
        }else{
            Toast.makeText(this@LoginActivity,"用户名或者密码错误",Toast.LENGTH_SHORT).show()
        }
    }

}
